package net.lovesrc.allin.jobs.backup;

import net.lovesrc.allin.biz.gt.GtService;
import net.lovesrc.allin.jobs.base.WorkTimeJob;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

import java.util.Date;

//@Component
@Profile("live")
public class GtRegisterJob extends WorkTimeJob {

    @Autowired
    private GtService gtService;

    @Override
    public void doRunOnce() {
        Date maxInsertTime = jdbcTemplate.queryForObject("select max(insertTime) from t_gt_account", Date.class);
        if (System.currentTimeMillis() - maxInsertTime.getTime() >= 47 * DateUtils.MILLIS_PER_MINUTE) {
            // 一天前的
            String registerPromotionCode = gtService.findNextRegisterPromotionCode();
            if (StringUtils.isNotBlank(registerPromotionCode)) {
                try {
                    gtService.newRegister(registerPromotionCode);
                } catch (Exception e) {
                    notificationService.syncEmail("GtJob Auto Register, " + e.getMessage());
                }
            }
        }
    }

    @Override
    protected long intervalMilliseconds() {
        return 1000 * RandomUtils.nextInt(60, 120);
    }

}
